use TestHelper;

sub testType{
    my $obj = shift;
    my $type = shift;
    

    print "\n";
    print "Comprobando tipo: $type\n";
    if( (ref $obj) ne $type){
	print "--->ERROR: ".(ref $obj)." != ".$type."\n";
    }else{
	print "    OK - ".(ref $obj)."\n";
    }
}

sub testParameters{
    my ($action, $params) = (shift, shift);
    
    print "Probando parametros\n"; 
    
    my $err = 0;
    foreach my $paramName (keys(%{$params})){
	my $fromAction = $action->params->{$paramName};
	my $fromParam = $params->{$paramName};
#	print "accion{".$paramName."}=".$fromAction."\n";
	if($fromAction ne $fromParam){
	    print "--->ERROR: accion{".$paramName."}=".$fromAction." != ".$fromParam."\n";
	}
    }

    print "    OK\n" unless($err);
}

sub testValidate{
    my $action = shift;

    print "Probando validación de parámetros\n";

    my $result = $action->__validate;
    
    if($result->is("SUCCESS")){
	print "    OK\n";
    }else{
	print "--->Error: ".$result->msg."\n";
    }

}

sub testResult{
    my $result = shift;
    
    print "Probando resultado\n";


    if($result->is("SUCCESS")){
	print "    OK - result is SUCCESS\n";
    }else{
	print "--->Error: ".$result->msg."\n";
    }
}

sub testPlacementOK{
    my $action = shift;

    my $map =  $action->params->{MAP};
    my ($x, $y) = ($action->params->{X}, $action->params->{Y});

    print "Probando ubicacion correcta en el mapa ($x; $y)\n";

    #print $map->tiles->[$x][$y]->contents->where($action->params->{ENTITY})."\n";
    
    if($map->tiles->[$x][$y]->contents->where($action->params->{ENTITY})){
	print "    OK\n";
    }else{
	print "--->ERROR: No se encontro en el mapa\n" ;
    }
    
}

sub testEntityPosition{
    my $entity = shift;
    my $map = shift;
    my ($x, $y) = (shift, shift);
    
    print "Probando Posicion\n";
    
    my $fromEntity = $entity->pos($map);

    if($fromEntity->[0] != $x){
	    print "--->ERROR: X = ".$fromEntity->[0]." != ".$x."\n";
    }else{
	if($fromEntity->[1] != $y){
	    print "--->ERROR: Y = ".$fromEntity->[1]." != ".$y."\n";
	}# y
	else{
	    print "    OK (".$fromEntity->[0]."; ".$fromEntity->[1].")\n";
	}
    }# x
}

sub drawMap{
    my ($map) = (shift);

    my $tiles = $map->tiles;

    my $tile;

    for(my $y = 0; $y < scalar(@{$tiles->[$x]}); $y++){
	for(my $x = 0; $x < scalar(@{$tiles}); $x++){
	    my $tile = $tiles->[$x][$y];
	    my $topEntity = $tile->contents->all->[0];

	    if(defined $topEntity){
		print($topEntity->pic);
	    }else{
		print($tile->pic);
	    }
	} # for x
	print("\n");
    } # for y
} # drawMap

1;
